﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Tomers.WPF.Shapes;

namespace epk23.PartIIProject
{
    public class GraphArrow
    {
        public int type { get; set; }
        public Arrow arrow { get; set; }

        public RuleNode startNode { get; set; }
        public RuleNode endNode { get; set; }

        public int startAnchor { get; set; }
        public int endAnchor { get; set; }

        public GraphArrow(RuleNode startNode, RuleNode endNode, Arrow arrow, int type)
        {
            this.type = type;
            this.startNode = startNode;
            this.endNode = endNode;
            this.startAnchor = startAnchor;
            this.endAnchor = endAnchor;
            this.arrow = arrow;

            //Anchor values: in clockwise order starting with 1 at the top.

            //Arrow type: type 0 is normal arrow for Increment nodes etc and >0 case for decrement nodes.
            //            type 1 is = 0 case for decrement nodes.

            setPosition();
        }

        public void setEndNode(RuleNode endNode)
        {
            this.endNode = endNode;
            setPosition();
        }

        public void setPositionExplicit(double x, double y)
        {
            arrow.X2 = x;
            arrow.Y2 = y;
        }

        public void setPosition()
        {

            double startX = (double)startNode.nodeBody.GetValue(Canvas.LeftProperty);
            double startY = (double)startNode.nodeBody.GetValue(Canvas.TopProperty);
            double endX = (double)endNode.nodeBody.GetValue(Canvas.LeftProperty);
            double endY = (double)endNode.nodeBody.GetValue(Canvas.TopProperty);

            double startHeight = startNode.nodeBody.Height;
            double startWidth = startNode.nodeBody.Width;
            double endHeight = endNode.nodeBody.Height;
            double endWidth = endNode.nodeBody.Width;

            //snapping

            if (startNode == endNode)
            {
                startAnchor = 3;
                endAnchor = 4;

                if (startNode.GetType() == typeof(DecrRuleNode) && type == 1)
                {
                    endAnchor = 2;
                }
            }
            else
            {
                {
                    int anchor1 = 0;
                    int anchor2 = 0;

                    double x1;
                    double y1;
                    double x2;
                    double y2;
                    double width1;
                    double width2;
                    double height1;
                    double height2;

                    for (int i = 1; i <= 2; i++)
                    {
                        if (i == 1)
                        {
                            x1 = startX;
                            y1 = startY;
                            height1 = startHeight;
                            width1 = startWidth;

                            x2 = endX;
                            y2 = endY;
                            height2 = endHeight;
                            width2 = endWidth;

                        }
                        else
                        {
                            x1 = endX;
                            y1 = endY;
                            height1 = endHeight;
                            width1 = endWidth;

                            x2 = startX;
                            y2 = startY;
                            height2 = startHeight;
                            width2 = startWidth;

                            anchor1 = endAnchor;
                            anchor2 = startAnchor;
                        }

                        if (x1 + width1 < x2)
                        {
                            anchor1 = 2;
                            anchor2 = 4;
                        }
                        else if (y1 + height1 < y2)
                        {
                            anchor1 = 3;
                            anchor2 = 1;
                        }
                        if (i == 1)
                        {
                            startAnchor = anchor1;
                            endAnchor = anchor2;
                        }
                        else
                        {
                            startAnchor = anchor2;
                            endAnchor = anchor1;
                        }
                    }
                }
            }
            switch (startAnchor)
            {
                case 1:
                    {
                        arrow.X1 = startX + startWidth / 2;
                        arrow.Y1 = startY;
                        break;
                    }
                case 2:
                    {
                        arrow.X1 = startX + startWidth;
                        arrow.Y1 = startY + startHeight / 2;
                        break;
                    }
                case 3:
                    {
                        arrow.X1 = startX + startWidth / 2;
                        arrow.Y1 = startY + startHeight;
                        break;
                    }
                case 4:
                    {
                        arrow.X1 = startX;
                        arrow.Y1 = startY + startHeight / 2;
                        break;
                    }
            }
            switch (endAnchor)
            {
                case 1:
                    {
                        arrow.X2 = endX + endWidth / 2;
                        arrow.Y2 = endY;
                        break;
                    }
                case 2:
                    {
                        arrow.X2 = endX + endWidth;
                        arrow.Y2 = endY + endHeight / 2;
                        break;
                    }
                case 3:
                    {
                        arrow.X2 = endX + endWidth / 2;
                        arrow.Y2 = endY + endHeight;
                        break;
                    }
                case 4:
                    {
                        arrow.X2 = endX;
                        arrow.Y2 = endY + endHeight / 2;
                        break;
                    }
            }
        }
    }
}
